
package com.lzb.compressmaster.dialog;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lxj.xpopup.core.AttachPopupView;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.adapter.SortAdapter;
import com.lzb.compressmaster.bean.SortBean;

import java.util.ArrayList;
import java.util.List;

public class SortDialog extends AttachPopupView implements View.OnClickListener {

    private RecyclerView rvSortList;
    private SortAdapter sortAdapter;
    private int selPosition;


    public SortDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_sort;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();
    }

    protected void initView() {
        rvSortList = findViewById(R.id.rvSortList);
        rvSortList.setLayoutManager(new LinearLayoutManager(getContext()));
        sortAdapter = new SortAdapter();
        rvSortList.setAdapter(sortAdapter);
        sortAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                sortAdapter.getItem(selPosition).setSelected(false);
                selPosition = position;
                sortAdapter.getItem(position).setSelected(true);
                sortAdapter.notifyDataSetChanged();
                dismiss();
                if (listener != null) {
                    listener.clickItem(sortAdapter.getItem(position).getName());
                }
            }
        });

        List<SortBean> list = new ArrayList<>();
        list.add(new SortBean(getContext().getString(R.string.sort_text1), true));
        list.add(new SortBean(getContext().getString(R.string.sort_text2), false));
        list.add(new SortBean(getContext().getString(R.string.sort_text3), false));
        list.add(new SortBean(getContext().getString(R.string.sort_text4), false));
        list.add(new SortBean(getContext().getString(R.string.sort_text5), false));
        sortAdapter.setList(list);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvEdit) {
            dismiss();
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickItem(String name);
    }

}
